package mango.functions;

import java.text.SimpleDateFormat;
import java.util.Date;

import mango.Mango;



public final class FuncDate {
    
    public static String parseDateTime(Date date) {
        String pattern = Mango.getProperty(Mango.DATEFORMAT) + " HH:mm:ss";
        return parse(pattern, date);
    }
    
    public static String parse(String pattern, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if (date != null) {
            return sdf.format(date);
        }
        return "";
    }
    
    public static String parse(Date date) {
        return parse(Mango.getProperty(Mango.DATEFORMAT), date);
    }
    
    public static String getCurrentDate(String pattern) {
        return parse(pattern, new Date());
    }
    
    public static String getCurrentDate() {
        return parse(new Date());
    }
    
    public static String getCurrentYear(){
        return getCurrentDate("yyyy");
    }
    
    public static String getCurrentMonth() {
        return getCurrentDate("MM");
    }
    
    public static String getMonth(Date date){
        return parse("yyyy-MM", date);
    }
    
    public static String getCurrentYearMonth() {
        return getCurrentDate("yyyy-MM");
    }
    
    public static String getCurrentTime() {
        return parse("HH:mm:ss", new Date());
    }
    
    public static Date toDate(String pattern, String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(date);
        }
        catch(Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public static Date toDate(String date) {
        return toDate(Mango.getProperty(Mango.DATEFORMAT), date);
    }
}
